//package com.lianziyou.chain.common.event;
//
//import io.reactivex.disposables.Disposable;
//import lombok.Getter;
//import lombok.extern.slf4j.Slf4j;
//import org.cloud.core.redis.RedisUtil;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.util.Assert;
//import org.springframework.util.StringUtils;
//import org.web3j.abi.EventEncoder;
//import org.web3j.abi.datatypes.Event;
//import org.web3j.protocol.Web3j;
//import org.web3j.protocol.core.DefaultBlockParameter;
//import org.web3j.protocol.core.DefaultBlockParameterName;
//import org.web3j.protocol.core.methods.request.EthFilter;
//import org.web3j.protocol.core.methods.response.Log;
//
//import java.math.BigInteger;
//
///**
// * 事件监听
// */
//@Slf4j
//public abstract class BaseEventFilter {
//
//    @Autowired
//    private Web3j web3j;
//    @Autowired
//    private RedisUtil redisUtil;
//    @Getter
//    private final String CACHE_LAST_BLOCK_KEY_PREFIX = "EventFilter:LastBlock:";
//    @Getter
//    private Disposable subscribe;
//
//    private String getCacheLastBlockKey(String contract, Event event){
//        return CACHE_LAST_BLOCK_KEY_PREFIX + contract + ":" + event.getName();
//    }
//
//    /**
//     * 添加监听
//     */
//    private void addFilter(String contract, Event event, DefaultBlockParameter startBlock, DefaultBlockParameter toBlock, String... optionalTopics){
//        if (StringUtils.isEmpty(contract) || event == null){
//            throw new IllegalArgumentException("合约地址获取事件为空");
//        }
//        Assert.isTrue(subscribe == null || isDisposed(), "订阅正在运行，不能重复添加");
//        EthFilter ethFilter = new EthFilter(startBlock, toBlock,contract)
//                .addSingleTopic(EventEncoder.encode(event))
//                .addOptionalTopics();
//        subscribe = web3j.ethLogFlowable(ethFilter).subscribe(log -> {
//            //处理回调
//            subscribeCallBack(log);
//            //记录最新区块
//            boolean setSuccess = redisUtil.set(getCacheLastBlockKey(contract, event), log.getBlockNumber());
//            if (!setSuccess){
//                throw new IllegalAccessException();
//            }
//        });
//    }
//
//    private void addFilter(String contract, Event event, BigInteger startBlock, BigInteger toBlock, String... optionalTopics){
//        addFilter(contract, event, DefaultBlockParameter.valueOf(startBlock), DefaultBlockParameter.valueOf(toBlock), optionalTopics);
//    }
//
//    private void addFilter(String contract, Event event, BigInteger startBlock, String... optionalTopics){
//        addFilter(contract, event, DefaultBlockParameter.valueOf(startBlock), DefaultBlockParameterName.LATEST, optionalTopics);
//    }
//
//    /**
//     * 监听到事件后的回调
//     * @param log
//     */
//    public abstract void subscribeCallBack(Log log);
//
//    /**
//     * 订阅是否关闭
//     * @return
//     */
//    public boolean isDisposed(){
//        Assert.notNull(subscribe, "订阅为空");
//        return subscribe.isDisposed();
//    }
//
//    /**
//     * 关闭订阅
//     */
//    public void stopSubscribe(){
//        Assert.isTrue(!isDisposed(), "订阅没有在运行");
//        subscribe.dispose();
//    }
//
//    public <T> T getEventFilter(){
//        return (T)this;
//    }
//
//}
